package sparkcore.day3.lesson01;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

/**
 * Created by Administrator on 2018/4/24.
 */
public class SecondaryTest {

    public static void main(String[] args) {
       SparkConf conf = new SparkConf().setMaster("local").setAppName("test");
       JavaSparkContext sc = new JavaSparkContext(conf);
        final JavaRDD<String> fileRDD = sc.textFile("D:\\1711班\\第八天\\资料\\sort.txt");
        fileRDD.mapToPair(new PairFunction<String, SecondarySortKey, String>() {
            @Override
            public Tuple2<SecondarySortKey, String> call(String line) throws Exception {
                final String[] fields = line.split(",");
                final SecondarySortKey secondarySortKey = new SecondarySortKey(
                        Integer.parseInt(fields[0]), Integer.parseInt(fields[1]));
                return new Tuple2<>(secondarySortKey,line);
            }
        }).sortByKey(true)
                .foreach(new VoidFunction<Tuple2<SecondarySortKey, String>>() {
                    @Override
                    public void call(Tuple2<SecondarySortKey, String> tuple) throws Exception {
                        System.out.println(tuple._2);
                    }
                });
    }
}
